<?php
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2007-2008 Starbugs (univis2typo3@googlegroups.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *  A copy is found in the textfile GPL.txt and important notices to the license
 *  from the author is found in LICENSE.txt distributed with these scripts.
 *
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
/**
 * Module: Web>UnivIS2Typo3
 *
 * Description:
 *
 * Notice:
 *
 *
 * @author	Starbugs <univis2typo3@googlegroups.com>
 */

global $BACK_PATH;

//require_once($BACK_PATH.'template.php');
require_once(t3lib_extMgm::extPath('univis2typo3').'/mod1/parser/sql/class.univistablemanager.php');
require_once(t3lib_extMgm::extPath('univis2typo3').'/mod1/parser/sql/class.univistablemanager_createtx.php');
require_once(t3lib_extMgm::extPath('univis2typo3').'/mod1/parser/sql/class.filltxtables.php');
require_once(t3lib_extMgm::extPath('univis2typo3').'/mod1/parser/univis2mysql.php');
require_once(t3lib_extMgm::extPath('univis2typo3').'/mod1/progressbar/class.progressbar.php');



global $DEBUG_MODE, $ERROR_MODE;
$DEBUG_MODE = false;
//$DEBUG_MODE = true;
$ERROR_MODE = true;

class Parser
{
    var $content;
    var $conf = array();
    function setUnivisPrgUrl($URL)
    {
	$this->conf['univis']['prg_url'] = $URL;
    }


    function renderContent()
    {
	$this->conf['parser']['tempfolder'] = '';

	// UnivIS Configuration
	$this->conf['univis']['department'] = '100105'; // UnvIS department ID (100106 = Inf2, 100105 = Inf5)
	//$this->conf['univis']['department'] = '090106'; // UnvIS department ID (100106 = Inf2, 100105 = Inf5)
	$this->conf['univis']['server'] = 'univis.uni-erlangen.de';
	$this->conf['univis']['prg_url'] = 'http://'.$this->conf['univis']['server'].'/prg?';


	// what data to import from UnivIS
	$this->conf['univis']['modules'] = array(
		'lectures' => true,
		'persons' => true,
		'chapters' => false,
		'departments' => true,
		'calendar' => false,
		'rooms' => true,
		'thesis' => true,
		'publications' => true,
		'projects' => true,
		);

	$univistablemanager = new UnivisTableManager();
	$TX_univistablemanager = new UnivisTableManager_createtx();
	$fillTXTables = new FillTXTables();

	$content .= '<h2>UnivIS Update Actions</h2>';
	$content .= '<ul>';
	$content .= '<li><a href="index.php?action=updateFromUnivis"> update from UnivIS </a></li>';
	$content .= '<li><a href="index.php?action=updateTXtables"> update TX tables </a></li>';
	$content .= '</ul>';

	$content .= '<h2>Installation actions</h2>';
	$content .= '<p>'
		. 'If you have a running installation of univis2typo3, please perform steps 1 and 2 to clean the database.'
		. 'If you have a fresh installation of the univis2typo3 plugin, please skip steps 1 and 2 and start with step 3.'
		. '</p>';
	$content .= '<ol>';
	$content .= '<li><a href="index.php?action=drop_univis_tables"> drop UnivIS tables </a></li>';
	$content .= '<li><a href="index.php?action=TX_drop_univis_tables"> TX drop UnivIS tables </a></li>';
	$content .= '</ol>';
	$content .= 'Now create the database schema, static filled tables and views for UnivIS and TX:<br />';
	$content .= '<ol start="3">';
	$content .= '<li><a href="index.php?action=create_db_schema"> create DB schema </a></li>';
	$content .= '<li><a href="index.php?action=create_db_static_tables"> create DB static tables </a></li>';
	$content .= '<li><a href="index.php?action=TX_create_db_schema"> TX create DB schema </a></li>';
	$content .= '<li><a href="index.php?action=TX_create_db_static_tables"> TX create DB static tables </a></li>';
	$content .= '</ol>';

	
	
	$content .= $this->conf['departments'];
//	$this->conf['univis']['departments'] = array ('100105', '100106', '100107', '100108');
 	$this->conf['univis']['departments'] = array ('100105');
 	//$this->conf['univis']['departments'] = array ('090106');

	$content .= '<BR><H2>Univis department IDs</H2><BR>';

	$content .= '<form action="select.htm">
		    <p>
		     <select name="Univis department IDs:" size="3">';

	foreach ($this->conf['univis']['departments'] as $department_id)
{
		$content .= '<option>';
		$content .= $department_id;
		$content .= '</option>';
}
	$content .= '</select>
		      </p>
		    </form><br />';

	echo $content;

	$this->conf['ProgressBar'] = new ProgressBar($this->doc->bgColor2);


	switch ($_GET['action'])
	{
	    case 'updateFromUnivis':
		$this->conf['ProgressBar']->setSteps(count($this->conf['univis']['departments']));
		$univistablemanager->delete_db_data();
		foreach ($this->conf['univis']['departments'] as $department_id)
		{
		    $this->conf['univis']['department'] = $department_id;
		    $this->conf['ProgressBar']->setUpdateInfo('Updating department with ID:' . $department_id . '...');
		    updateFromUnivis($this->conf);
		    $this->conf['ProgressBar']->nextStep();
		}
		
		$this->conf['ProgressBar']->setUpdateInfo('Also gut. Succeeded');
		break;

	    case 'create_db_schema':
		$this->conf['ProgressBar']->setUpdateInfo('createDBschema');
		$univistablemanager->create_db_schema();
		$this->conf['ProgressBar']->nextStep();
		$this->conf['ProgressBar']->setUpdateInfo('createDBschema succeeded');
		break;

	    case 'create_db_static_tables':
		$this->conf['ProgressBar']->setUpdateInfo('createDBstaticTables');
		$univistablemanager->create_db_static_tables();
		$this->conf['ProgressBar']->nextStep();
		$this->conf['ProgressBar']->setUpdateInfo('createDBstaticTables succeeded<');
		break;

	    case 'drop_univis_tables':
		$this->conf['ProgressBar']->setUpdateInfo('dropUnivisTables');
		$univistablemanager->drop_univis_tables();
		$this->conf['ProgressBar']->nextStep();
		$this->conf['ProgressBar']->setUpdateInfo('dropUnivisTables succeeded<');
		break;

	    case 'TX_create_db_schema':
		$this->conf['ProgressBar']->setUpdateInfo('TX_createDBschema');
		$TX_univistablemanager->create_db_schema();
		$this->conf['ProgressBar']->nextStep();
		$this->conf['ProgressBar']->setUpdateInfo('TX_createDBschema succeeded');
		break;

	    case 'TX_create_db_static_tables':
		$this->conf['ProgressBar']->setUpdateInfo('TX_createDBstaticTables');
		$TX_univistablemanager->create_db_static_tables();
		$this->conf['ProgressBar']->nextStep();
		$this->conf['ProgressBar']->setUpdateInfo('TX_createDBstaticTables succeeded<');
		break;

	    case 'TX_drop_univis_tables':
		$this->conf['ProgressBar']->setUpdateInfo('TX_dropUnivisTables');
		$TX_univistablemanager->drop_univis_tables();
		$this->conf['ProgressBar']->nextStep();
		$this->conf['ProgressBar']->setUpdateInfo('TX_dropUnivisTables succeeded<');
		break;

	    case 'updateTXtables':
		$this->conf['ProgressBar']->setSteps(2);
		$this->conf['ProgressBar']->setUpdateInfo('deleteTXdata');
		$TX_univistablemanager->delete_db_data();
		$this->conf['ProgressBar']->nextStep();
		$this->conf['ProgressBar']->setUpdateInfo('updateTXtables');
		$fillTXTables->copyTables();
		$this->conf['ProgressBar']->nextStep();
		$this->conf['ProgressBar']->setUpdateInfo('updateTXtables succeeded');
		break;


	    default:
	}
    }

}
?>
